#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include "card_table.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#define DISP_BUF_SIZE (128 * 1024)

 lv_obj_t * rbtn;
lv_obj_t * lbtn;
lv_obj_t* win;

void show_main_win()
{
     lv_obj_t*mainwin = lv_obj_create(lv_scr_act());
    //设置窗口的大小
    lv_obj_set_size(mainwin, 800, 480);
    //设置窗口的位置
    lv_obj_align(mainwin, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *tabview;
    tabview = lv_tabview_create(mainwin, LV_DIR_TOP, 50);

    /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
    lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab 1");
    lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab 2");
    lv_obj_t *tab3 = lv_tabview_add_tab(tabview, "Tab 3");

    /*Add content to the tabs*/
    lv_obj_t * label = lv_label_create(tab1);
    lv_label_set_text(label, "This the first tab\n\n");

    label = lv_label_create(tab2);
    lv_label_set_text(label, "Second tab");

    label = lv_label_create(tab3);
    lv_label_set_text(label, "Third tab");

    lv_obj_scroll_to_view_recursive(label, LV_ANIM_ON);
}

void event_handler(lv_event_t *e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    if(obj == rbtn)
    {
        printf("register\n");
    }
    else if(obj == lbtn)
    {
        printf("login\n");

        //跳转到主界面
        //1关闭当前界面
        lv_obj_del(win);
        //2 创建主界面
        show_main_win();
    }
}

void show_win()
{
    //创建一个窗口
     win = lv_obj_create(lv_scr_act());
    //设置窗口的大小
    lv_obj_set_size(win, 800, 480);
    //设置窗口的位置
    lv_obj_align(win, LV_ALIGN_CENTER, 0, 0);


    //注册按钮
    rbtn = lv_btn_create(win);
    //设置按钮的大小
    lv_obj_set_size(rbtn, 150, 100);
    //设置按钮的位置
    lv_obj_align(rbtn, LV_ALIGN_CENTER, -100, 0);
    //设置按钮的文本
    lv_obj_t * rbtn_label = lv_label_create(rbtn);
    lv_label_set_text(rbtn_label, "register");
    //注册按钮的回调函数
    lv_obj_add_event_cb(rbtn, event_handler,LV_EVENT_CLICKED,NULL);

    //登录按钮
    lbtn = lv_btn_create(win);
    //设置按钮的大小
    lv_obj_set_size(lbtn, 150, 100);
    //设置按钮的位置
    lv_obj_align(lbtn, LV_ALIGN_CENTER, 100, 0);
    //设置按钮的文本
    lv_obj_t * lbtn_label = lv_label_create(lbtn);
    lv_label_set_text(lbtn_label, "login");
    //注册按钮的回调函数
    lv_obj_add_event_cb(lbtn, event_handler,LV_EVENT_CLICKED,NULL);
}

int main(int argc,char*argv[])
{
    /*LittlevGL init 初始化LVGL库 */
    lv_init();

    /*Linux frame buffer device init 输出设备（显示器/ 显示驱动器）LCD初始化*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800; //液晶屏的分辨率  宽度像素点
    disp_drv.ver_res    = 480; //液晶屏的分辨率  高度像素点
    lv_disp_drv_register(&disp_drv); //将LCD屏设备信息注册到lvgl内。让lvgl能够使用我们的屏幕

    evdev_init(); //触摸屏设备初始化
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1); //将以上准备好的触摸屏设备信息注册到lvgl,让lvgl能使用触摸屏


    /*Set a cursor for the mouse 追踪鼠标轨迹*/
    LV_IMG_DECLARE(mouse_cursor_icon)
    lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/

    /*Create a Demo 我们要修改的地方*/
    //lv_demo_widgets();
    // lv_obj_t*srceen = lv_scr_act();//获取当前屏幕对象
    // //在当前屏幕对象上创建一个子对象
    // lv_obj_t *child = lv_obj_create(srceen);
    // //设置子对象的大小
    // lv_obj_set_size(child,100,100);
    // //设置子对象的背景颜色
    // lv_obj_set_style_bg_color(child,lv_color_hex(0xff0000),0);
    // //设置子对象的位置
    // //lv_obj_set_pos(child,200,100);
    // //设置子对象在父对象的中心
    // lv_obj_set_align(child,LV_ALIGN_CENTER,-50,0);
    //lv_example_btn_1();

    // 连接服务器
    int port = atoi(argv[2]);
    client_connect(argv[1],port);
    
    welcome_page_display();
    // pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        pthread_mutex_lock(&mutex);
        lv_timer_handler(); //事件处理
        pthread_mutex_unlock(&mutex);
        usleep(5000); //5毫秒-循环的频次  心跳节拍
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
